<?php
/**
 * @author Mazvv
 * @package Currencies
 */
class Currencies extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'currencies';
		$this->lang->load('currencies');
		$this->load->model('currencies_model');
		
		$this->a_validation_rules['code'] = "required";
		$this->a_validation_rules['currency_name'] = "required";
		$this->a_validation_rules['left_word'] = "required";
		$this->a_validation_rules['right_word'] = "required";
		
		$this->a_validation_fields['code'] = $this->lang->line('CODE');
		$this->a_validation_fields['currency_name'] = $this->lang->line('NAME');
		$this->a_validation_fields['left_word'] = $this->lang->line('LEFT_WORD');
		$this->a_validation_fields['right_word'] = $this->lang->line('RIGHT_WORD');
		
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_Name){
		switch ($m_Name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}			
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}
	/*
	private function GetTools(){
		$tools['new'] = $this->load->view('currencies/tools/new', null, True);
		$tools['edit'] = $this->load->view('currencies/tools/edit', null, True);
		$tools['details'] = $this->load->view('currencies/tools/details', null, True);
        $tools['remove'] = $this->load->view('currencies/tools/remove', null, True);;
        $tools['refresh'] = $this->ajax->link_to_remote($this->lang->line('REFRESH'), array('url'=>'currencies/uJ', 'update'=>'currencies_journal', 'confirm'=>$this->lang->line('REFRESH_CONFIRM')));
        return $tools;
	}
	*/
	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 
		$content['code'] =  $this->lang->line('CODE');
		$content['currency_name'] =  $this->lang->line('NAME');
		$content['left_word'] =  $this->lang->line('LEFT_WORD');
		$content['right_word'] =  $this->lang->line('RIGHT_WORD');
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->currencies_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if(!$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('currencies/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE'); 
			$content['rid'] = 'ID'; 
			$content['code'] =  $this->lang->line('CODE');
			$content['code_field'] =  form_input('code', $this->validation->code?$this->validation->code:'');
			$content['currency_name'] =  $this->lang->line('NAME');
			$content['currency_name_field'] =  form_input('currency_name', $this->validation->currency_name?$this->validation->currency_name:'');
			$content['left_word'] =  $this->lang->line('LEFT_WORD');
			$content['left_word_field'] =  form_input('left_word', $this->validation->left_word?$this->validation->left_word:'');
			$content['right_word'] =  $this->lang->line('RIGHT_WORD');
			$content['right_word_field'] =  form_input('right_word', $this->validation->right_word?$this->validation->right_word:'');
			$content['descr'] =  $this->lang->line('DESCR');
			$content['descr_field'] =  form_input('descr', $this->validation->descr?$this->validation->descr:'');
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			return $this->load->view('currencies/create', $content, True);
		}
		if($insRid = $this->currencies_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->currencies_model->GetEditDS();
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT'); 
			$content['rid'] = 'ID';
			$content['code'] =  $this->lang->line('CODE');
			$content['code_field'] =  form_input('code', $this->validation->code?$this->validation->code:$content['DS']->code);
			$content['currency_name'] =  $this->lang->line('NAME');
			$content['currency_name_field'] =  form_input('currency_name', $this->validation->currency_name?$this->validation->currency_name:$content['DS']->currency_name);
			$content['left_word'] =  $this->lang->line('LEFT_WORD');
			$content['left_word_field'] =  form_input('left_word', $this->validation->left_word?$this->validation->left_word:$content['DS']->left_word);
			$content['right_word'] =  $this->lang->line('RIGHT_WORD');
			$content['right_word_field'] =  form_input('right_word', $this->validation->right_word?$this->validation->right_word:$content['DS']->right_word);
			$content['descr'] =  $this->lang->line('DESCR');
			$content['descr_field'] =  form_input('descr', $this->validation->descr?$this->validation->descr:$content['DS']->descr);
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			return $this->load->view('currencies/edit', $content, True);
		}
		if($this->currencies_model->UpdateRecord()){
			$this->load->view('currencies/edit_success', null);
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->currencies_model->GetEditDS();
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			$content['rid'] = 'ID'; 
			$content['code'] =  $this->lang->line('CODE');
			$content['code_field'] =  form_input('code', $this->validation->code?$this->validation->code:$content['DS']->code);
			$content['currency_name'] =  $this->lang->line('NAME');
			$content['currency_name_field'] =  form_input('currency_name', $this->validation->currency_name?$this->validation->currency_name:$content['DS']->currency_name);
			$content['left_word'] =  $this->lang->line('LEFT_WORD');
			$content['left_word_field'] =  form_input('left_word', $this->validation->left_word?$this->validation->left_word:$content['DS']->left_word);
			$content['right_word'] =  $this->lang->line('RIGHT_WORD');
			$content['right_word_field'] =  form_input('right_word', $this->validation->right_word?$this->validation->right_word:$content['DS']->right_word);
			$content['descr'] =  $this->lang->line('DESCR');
			$content['descr_field'] =  form_input('descr', $this->validation->descr?$this->validation->descr:$content['DS']->descr);
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			return $this->load->view('currencies/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->currencies_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
	
	/* { SERVICE */
/*	public function _sort(){
		$p_column = $this->uri->segment(3);
		$sort = array('c'=>$p_column, 'r'=>'ASC');
		if($oldSort = $this->GetSessionData('sort')){
			if($oldSort['c']==$p_column) {
				$sort['r'] = ($oldSort['r']=='ASC')?'DESC':'ASC';
			}
		}
		$this->SetSessionData('sort', $sort);
		echo $this->_journal();
	}
	
	public function _activerecord(){
		$this->SetSessionData('activerecord', $this->uri->segment(3));
		echo $this->_journal();
	}

	public function _setactiverecord(){
		$this->SetSessionData('activerecord', $this->uri->segment(3));
	}
	
*/	/* } SERVICE */	
}

?>